import { defineStore } from 'pinia';
import { reqUserInfo, reqLogout } from '@/api/login';
import { removeToken } from '@/utils/auth';
import 'element-plus/es/components/message/style/css';
import router from '@/router/index';
export const useUserInfoStore = defineStore('user', {
  state: () => {
    return {
      userInfo: {},
    };
  },
  actions: {
    // 获取用户信息
    async getUserInfo() {
      //发送请求
      const result = await reqUserInfo();
      if (result.code == 200) {
        // 获取用户信息成功
        this.userInfo = result.data;
      } else {
        // 获取用户信息失败
        ElMessage.error(result.message);
      }
    },

    //退出登陆并清除用户信息
    async clearUserInfo(msg) {
      const result = await reqLogout();
      if (result.code == 200) {
        this.userInfo = {};
        // 删除token
        removeToken();
        // 回到登录界面
        router.push('/login');
      } else {
      }
    },
  },
});
